package com.yh.autobio.fragment;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


import com.yh.autobio.AppContext;
import com.yh.autobio.R;
import com.yh.autobio.base.BaseFragment;
import com.yh.autobio.common.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：zhanghang
 * <p>
 * 工程：hlj
 * <p>
 * 文件名：Ip_port_Fragment
 * <p>
 * 创建时间：2017.10.9.14:07
 */

public class Ip_Fragment extends BaseFragment {

    @InjectView(R.id.et_ip)
    EditText etIp;
    @InjectView(R.id.btn_save)
    Button btn_save;

    private String mTvIps = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(mActivity, R.layout.fragment_ip, null);
        ButterKnife.inject(this, view);
        initView(view);
        initData();
        return view;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        btn_save.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_save:
                mTvIps = etIp.getText().toString();
                if (!StringUtils.isEmpty(mTvIps)) {
                    AppContext.getInstance().setProperty("set_ip", mTvIps);
                    Snackbar.make(mActivity.getWindow().getDecorView(), "设置成功", Snackbar.LENGTH_SHORT).show();
//                    boolean ipok = StringUtils.ipCheck(mTvIps);
//                    if (ipok) {
//
//                        startSocket(mTvIps, mTvPorts);
//
//                    } else {
//                        Snackbar.make(mActivity.getWindow().getDecorView(), "Ip不合法！", Snackbar.LENGTH_SHORT).show();
//                    }
                } else {
                    Snackbar.make(mActivity.getWindow().getDecorView(), "必填信息不能为空！", Snackbar.LENGTH_SHORT).show();
                }
                break;

        }
    }

    @Override
    public void initData() {
        super.initData();
        etIp.setText(AppContext.getInstance().getProperty("set_ip"));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
